//
//  GDPhotoManager.m
//  GuiderHealth
//
//  Created by zyming on 2022/9/5.
//  Copyright © 2022 YM&EF. All rights reserved.
//

#import "GDPhotoManager.h"
#import "HXPhotoManager.h"
#import "UIViewController+HXExtension.h"

@interface GDPhotoManager ()

@property(nonatomic, strong) HXPhotoManager *manager;

@end

static GDPhotoManager *sharedSingleton = nil;
@implementation GDPhotoManager

+ (GDPhotoManager *)sharedManager {
    if (sharedSingleton == nil) {
        @synchronized (self) {
            if (sharedSingleton == nil) {
                sharedSingleton = [[GDPhotoManager alloc] init];
            }
        }
    }
    return sharedSingleton;
}
// 懒加载 照片管理类
- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.configuration.saveSystemAblum = YES;
        _manager.configuration.photoMaxNum = 1;
        _manager.configuration.openCamera = YES;
        _manager.configuration.cameraPhotoJumpEdit = YES;
    //    _manager.configuration.videoMaxNum = 1;
        _manager.configuration.maxNum = 1;
    //    _manager.configuration.selectTogether = NO;
        _manager.configuration.photoCanEdit = YES;
    //    _manager.configuration.videoCanEdit = NO;
    }
    return _manager;
}
- (void)takePhotoWithBlock:(ImageBlock)imageBlock{
    
//    HXWeakSelf
//    [self.getCurrentViewController hx_presentCustomCameraViewControllerWithManager:self.manager delegate:self];
    [self.getCurrentVC hx_presentCustomCameraViewControllerWithManager:self.manager done:^(HXPhotoModel *model, HXCustomCameraViewController *viewController) {
        
        UIImage *image = model.previewPhoto;
        imageBlock(image);
//        [weakSelf finishImage:image];
        
    } cancel:^(HXCustomCameraViewController *viewController) {
        NSSLog(@"block - 取消了");
    }];
}
- (void)choosePhotoWithBlock:(ImageBlock)imageBlock{
    
    [self.getCurrentVC hx_presentSelectPhotoControllerWithManager:self.manager didDone:^(NSArray<HXPhotoModel *> *allList, NSArray<HXPhotoModel *> *photoList, NSArray<HXPhotoModel *> *videoList, BOOL isOriginal, UIViewController *viewController, HXPhotoManager *manager) {
        UIImage *image = photoList.firstObject.previewPhoto;
        imageBlock(image);
    } cancel:^(UIViewController *viewController, HXPhotoManager *manager) {
        NSSLog(@"block - 取消了");
    }];

}


- (UIViewController *)getCurrentVC{

    UIWindow * window = [[UIApplication sharedApplication] keyWindow];

    if (window.windowLevel != UIWindowLevelNormal){

        NSArray *windows = [[UIApplication sharedApplication] windows];

        for(UIWindow * tmpWin in windows){

            if (tmpWin.windowLevel == UIWindowLevelNormal){

                window = tmpWin;

                break;

            }

        }

    }

    UIViewController *result = window.rootViewController;

    while (result.presentedViewController) {

        result = result.presentedViewController;

    }

    if ([result isKindOfClass:[UITabBarController class]]) {

        result = [(UITabBarController *)result selectedViewController];

    }

    if ([result isKindOfClass:[UINavigationController class]]) {

        result = [(UINavigationController *)result topViewController];

    }

    return result;
}
@end
